package net.lainiao.mycost.service.impl;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import net.lainiao.mycost.bean.SalaryPayment;
import net.lainiao.mycost.dao.ISalaryPaymentDAO;
import net.lainiao.mycost.service.ISalaryPaymentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Parameter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */
@Service
@Transactional
public class SalaryPaymentServiceImpl implements ISalaryPaymentService {

    @Autowired
    private ISalaryPaymentDAO dao;

    @Override
    public void add(SalaryPayment payment) {
        dao.add(payment);
    }

    public void condition(SalaryPayment payment){
        if(payment!=null){
            if(StringUtils.isNotEmpty(payment.getUserName())){
                payment.setUserName("%"+payment.getUserName()+"%");
            }
        }
    }

    @Override
    public void addAll(InputStream inputStream) throws IOException, BiffException, ParseException {
        Workbook book=Workbook.getWorkbook(inputStream);
        Sheet sheet=book.getSheet(0);
        System.out.println(sheet.getColumns());
        System.out.println(sheet.getRows());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 1; i <sheet.getRows() ; i++) {
            SalaryPayment salaryPayment=new SalaryPayment();
            Cell idcell=sheet.getCell(0,i);
            Integer userId=Integer.parseInt(idcell.getContents());
            salaryPayment.setUserId(userId);

            //发放时间
            Cell timeCell=sheet.getCell(2,i);
            String time=timeCell.getContents();
            Date paymentTime=format.parse(time);
            salaryPayment.setPaymentTime(paymentTime);

            Cell moneyCell=sheet.getCell(3,i);
            Double paymentMoney=Double.parseDouble(moneyCell.getContents());
            salaryPayment.setPaymentMoney(paymentMoney);

            dao.add(salaryPayment);
        }

    }

    @Override
    public List<SalaryPayment> list(SalaryPayment payment) {
        condition(payment);
        return dao.list(payment);
    }

    @Override
    public long getCount(SalaryPayment payment) {
        condition(payment);
        return dao.getCount(payment);
    }
}
